import Vue from 'vue'
import Router from 'vue-router'


Vue.use(Router)

import Login from '@/views/login/login'

import Home from '@/views/home/index'

import Test from '@/views/test/test'


import Users from '@/views/users/users'

import Goods from '@/views/goods/goods'


import Roles from '@/views/roles/roles'

const router = new Router({
  routes: [
    {
      path: '/', redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/home',
      name: 'home',
      component: Home,
      

      children:[
        {path:'/home',redirect:'/users'},
        {path:'/users',component:Users},
        {path:'/goods',component:Goods},
        {path:'/roles',component:Roles}

       
      ]
    },
    {
      path: '/test',
      name: 'test',
      component: Test
    }
  ]
})



// 3. 路由导航守卫控制访问权限
// 如果用户没有登录，但是直接通过URL访问特定页面，需要重新导航到登录页面

//基础代码
// router.beforeEach((to, from, next) => {
//   // 要去登录页面 
//   if (to.path == '/login') {
//     return next()
//   }

//   var token = window.sessionStorage.getItem('token')
//   if (token) {
//     return next()
//   } else {
//     return next('/login')
//   }

// })

// 优化一下


router.beforeEach((to, from, next) => {
  // 要去登录页面 
  if (to.path == '/login') return next()
  // 判断是否有token 
  var token = window.sessionStorage.getItem('token')
  if (token) return next()  // 如果有就不阻止你
  return next('/login')  // 每天token  那么登录页面进行登录

})








export default router

